//
//  MGDatePickView.m
//  MGPublic
//
//  Created by lhongl on 2025/10/24.
//

#import "MGDatePickView.h"
#import "MGMacros.h"
#import <Masonry/Masonry.h>
@interface MGDatePickView ()

///
@property (nonatomic, strong) UIDatePicker  *pickView;

///
@property (nonatomic, strong) UIToolbar  *toolbar;

///
@property (nonatomic, strong) NSDateFormatter *fromatter;

@end

@implementation MGDatePickView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
    // Do any additional setup after loading the view.
}

- (void)setUpView {
    [self.view addSubview:self.toolbar];
    [self.view addSubview:self.pickView];
    [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.centerY.equalTo(self.view.mas_centerY).offset(20);
    }];
}


#pragma mark - 当前模式
- (void)setDatePickerMode:(UIDatePickerMode)datePickerMode{
    _datePickerMode = datePickerMode;
    self.pickView.datePickerMode = datePickerMode;
}

#pragma mark - max时间
- (void)setMaxDate:(NSDate *)maxDate{
    if(maxDate == NULL){
        return;
    }
    _maxDate = maxDate;
    [self.pickView setMaximumDate:maxDate];
}
- (void)setMaxDateStr:(NSString *)maxDateStr {
    if(maxDateStr == NULL || maxDateStr.length == 0){
        return;
    }
    _maxDateStr = maxDateStr;
    NSDate *date = [self.fromatter dateFromString:maxDateStr];
    [self.pickView setMaximumDate:date];
}

#pragma mark - min时间
- (void)setMinDate:(NSDate *)minDate{
    if(minDate == NULL){
        return;
    }
    _minDate = minDate;
    [self.pickView setMinimumDate:minDate];
}

- (void)setMiniDateStr:(NSString *)miniDateStr {
    if(miniDateStr == NULL || miniDateStr.length == 0){
        return;
    }
    _miniDateStr = miniDateStr;
    NSDate *date = [self.fromatter dateFromString:miniDateStr];
    [self.pickView setMinimumDate:date];
}

#pragma mark -设置选中时间
- (void)setSelectDate:(NSDate *)selectDate {
    if(selectDate == NULL){
        return;
    }
    _selectDate = selectDate;
    [self.pickView setDate:selectDate];
}

- (void)setSelectDateStr:(NSString *)selectDateStr {
    if(selectDateStr == NULL || selectDateStr.length == 0){
        return;
    }
    _selectDateStr = selectDateStr;
    NSDate *date = [self.fromatter dateFromString:selectDateStr];
    [self.pickView setDate:date];
}


#pragma mark 时间戳
- (void)setDateFromatter:(NSString *)dateFromatter{
    if(dateFromatter == NULL || dateFromatter.length == 0){
        return;
    }
    _dateFromatter = dateFromatter;
    [_fromatter setDateFormat:dateFromatter];
}

#pragma mark 事件
/// 取消
- (void)cancelItemAction {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

/// 确认
- (void)sureItemAction {
    NSDate *date = self.pickView.date;
    NSString *result = [self.fromatter stringFromDate:date];
    if([self.delegate respondsToSelector:@selector(pickView:result:)]){
        [self.delegate pickView:self result:result];
    }
    [self cancelItemAction];
}

#pragma mark 懒加载
- (UIToolbar *)toolbar {
    if (!_toolbar) {
        _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
        _toolbar.clipsToBounds = YES;
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelItemAction)];

        cancelItem.width = 80;
        UIBarButtonItem *spaceItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        UIBarButtonItem *sureItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStyleDone target:self action:@selector(sureItemAction)];
        sureItem.width = 80;
        _toolbar.items = @[cancelItem,spaceItem,sureItem];
        _toolbar.barTintColor = [UIColor whiteColor];
        _toolbar.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
    }
    return _toolbar;
}

- (UIDatePicker *)pickView{
    if (!_pickView) {
        _pickView = [[UIDatePicker alloc] init];
        _pickView.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        _pickView.datePickerMode = UIDatePickerModeDate;
        if (@available(iOS 13.4, *)) {
            _pickView.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
    }
    return _pickView;
}

- (NSDateFormatter *)fromatter{
    if(!_fromatter){
        _fromatter = [[NSDateFormatter alloc] init];
        [_fromatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _fromatter;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
